/*
 *	WARN  :  THIS BACKEND CODE IS AUTO GENERATE, ALL RIGHTS RESERVED.
 *	AUTHOR:  ZhaoChunPing
 *	EMAIL :  forxy@126.com
 *	DATE  :  2022.1
 */

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "pkcs11.h"
#include "msg_fmt.h"
#include "auto_inner_pkcs11.h"

CK_RV	C_LoginUser_inner
(
	void *ctx,
	CK_USER_TYPE userType,
	CK_UTF8CHAR_PTR pPin,
	CK_ULONG ulPinLen,
	CK_UTF8CHAR_PTR pUsername,
	CK_ULONG ulUsernameLen
)
{
	char	str[256] = {0};
	printf("%s......\n", __FUNCTION__);
	printf("userType: %ld\n", userType);
	memcpy(str, pPin, ulPinLen);
	printf("pPin: %s\n", str);
	printf("ulPinLen: %ld\n", ulPinLen);
	memset(str, 0, 256);
	memcpy(str, pUsername, ulUsernameLen);
	printf("pUsername: %s\n", str);
	printf("ulUsernameLen: %ld\n", ulUsernameLen);
	printf("%s ok.\n", __FUNCTION__);
	return CKR_OK;
}

CK_RV	C_Logout_inner
(
	void *ctx
)
{
	printf("%s ok.\n", __FUNCTION__);
	return CKR_OK;
}

CK_RV	C_EncryptInit_inner
(
	void *ctx,
	CK_MECHANISM_PTR pMechanism,
	CK_OBJECT_HANDLE hKey
)
{
	char	str[256] = {0};
	printf("%s......\n", __FUNCTION__);
	printf("pMechanism type: %ld\n", pMechanism->mechanism);
	printf("pMechanism len: %ld\n", pMechanism->ulParameterLen);
	memcpy(str, pMechanism->pParameter, pMechanism->ulParameterLen);
	printf("pMechanism value: %s\n", str);
	printf("key: %ld\n", hKey);
	printf("%s ok.\n", __FUNCTION__);
	return CKR_OK;
}

CK_RV	C_EncryptUpdate_inner
(
	void *ctx,
	CK_BYTE_PTR pPart,
	CK_ULONG ulPartLen,
	CK_BYTE_PTR pEncryptedPart,
	CK_ULONG_PTR pulEncryptedPartLen
)
{
	printf("%s......\n", __FUNCTION__);
	*pulEncryptedPartLen = ulPartLen;
	memcpy(pEncryptedPart, pPart, ulPartLen);
	printf("%s ok.\n", __FUNCTION__);
	return CKR_OK;
}

CK_RV	C_EncryptFinal_inner
(
	void *ctx,
	CK_BYTE_PTR pLastEncryptedPart,
	CK_ULONG_PTR pulLastEncryptedPartLen
)
{
	printf("%s......\n", __FUNCTION__);
	strcpy((char *)pLastEncryptedPart, "ENC final.\n");
	*pulLastEncryptedPartLen = 10;
	printf("%s ok.\n", __FUNCTION__);
	return CKR_OK;
}

CK_RV	C_Encrypt_inner
(
	void *ctx,
	CK_BYTE_PTR pData,
	CK_ULONG ulDataLen,
	CK_BYTE_PTR pEncryptedData,
	CK_ULONG_PTR pulEncryptedDataLen
)
{
	printf("%s......\n", __FUNCTION__);
	*pulEncryptedDataLen = ulDataLen;
	memcpy(pEncryptedData, pData, ulDataLen);
	printf("%s ok.\n", __FUNCTION__);
	return CKR_OK;
}

CK_RV	C_DecryptInit_inner
(
	void *ctx,
	CK_MECHANISM_PTR pMechanism,
	CK_OBJECT_HANDLE hKey
)
{
	char	str[256] = {0};
	printf("%s......\n", __FUNCTION__);
	printf("pMechanism type: %ld\n", pMechanism->mechanism);
	printf("pMechanism len: %ld\n", pMechanism->ulParameterLen);
	memcpy(str, pMechanism->pParameter, pMechanism->ulParameterLen);
	printf("pMechanism value: %s\n", str);
	printf("key: %ld\n", hKey);
	printf("%s ok.\n", __FUNCTION__);
	return CKR_OK;
}

CK_RV	C_DecryptUpdate_inner
(
	void *ctx,
	CK_BYTE_PTR pEncryptedPart,
	CK_ULONG ulEncryptedPartLen,
	CK_BYTE_PTR pPart,
	CK_ULONG_PTR pulPartLen
)
{
	printf("%s......\n", __FUNCTION__);
	*pulPartLen = ulEncryptedPartLen;
	memcpy(pPart, pEncryptedPart, ulEncryptedPartLen);
	printf("%s ok.\n", __FUNCTION__);
	return CKR_OK;
}

CK_RV	C_DecryptFinal_inner
(
	void *ctx,
	CK_BYTE_PTR pLastPart,
	CK_ULONG_PTR pulLastPartLen
)
{
	printf("%s......\n", __FUNCTION__);
	strcpy((char *)pLastPart, "DECF.\n");
	*pulLastPartLen = 4;
	printf("%s ok.\n", __FUNCTION__);
	return CKR_OK;
}

CK_RV	C_Decrypt_inner
(
	void *ctx,
	CK_BYTE_PTR pEncryptedData,
	CK_ULONG ulEncryptedDataLen,
	CK_BYTE_PTR pData,
	CK_ULONG_PTR pulDataLen
)
{
	printf("%s......\n", __FUNCTION__);
	memcpy(pData, pEncryptedData, ulEncryptedDataLen);
	*pulDataLen = ulEncryptedDataLen;
	printf("%s ok.\n", __FUNCTION__);
	return CKR_OK;
}

CK_RV	C_DigestInit_inner
(
	void *ctx,
	CK_MECHANISM_PTR pMechanism
)
{
	printf("%s......\n", __FUNCTION__);
	printf("pMechanism type: %ld\n", pMechanism->mechanism);
	printf("pMechanism len: %ld\n", pMechanism->ulParameterLen);
	printf("%s ok.\n", __FUNCTION__);
	return CKR_OK;
}

CK_RV	C_DigestUpdate_inner
(
	void *ctx,
	CK_BYTE_PTR pPart,
	CK_ULONG ulPartLen
)
{
	char	str[256] = {0};
	printf("%s......\n", __FUNCTION__);
	printf("C_DigestUpdate len: %ld\n", ulPartLen);
	memcpy(str, pPart, ulPartLen);
	printf("C_DigestUpdate: %s\n", str);
	printf("%s ok.\n", __FUNCTION__);
	return CKR_OK;
}

CK_RV	C_DigestFinal_inner
(
	void *ctx,
	CK_BYTE_PTR pDigest,
	CK_ULONG_PTR pulDigestLen
)
{
	printf("%s......\n", __FUNCTION__);
	strcpy((char *)pDigest, "DRESULT");
	*pulDigestLen = 7;
	printf("%s ok.\n", __FUNCTION__);
	return CKR_OK;
}

CK_RV	C_Digest_inner
(
	void *ctx,
	CK_BYTE_PTR pData,
	CK_ULONG ulDataLen,
	CK_BYTE_PTR pDigest,
	CK_ULONG_PTR pulDigestLen
)
{
	char	str[256] = {0};
	printf("%s......\n", __FUNCTION__);
	printf("inputlen: %ld\n", ulDataLen);
	memcpy(str, pData, ulDataLen);
	printf("input: %s\n", str);
	*pulDigestLen = 16;
	memset(pDigest, 0x31, 16);
	printf("%s ok.\n", __FUNCTION__);
	return CKR_OK;
}

CK_RV	C_Sign_inner
(
	void *ctx,
	CK_BYTE_PTR pData,
	CK_ULONG ulDataLen,
	CK_BYTE_PTR pSignature,
	CK_ULONG_PTR pulSignatureLen
)
{
	char	str[256] = {0};
	printf("%s......\n", __FUNCTION__);
	memcpy(str, pData, ulDataLen);
	printf("input len: %ld\n", ulDataLen);
	printf("input: %s\n", str);
	memset(pSignature, 0x32, 16);
	*pulSignatureLen = 16;
	printf("%s ok.\n", __FUNCTION__);
	return CKR_OK;
}

CK_RV	C_Verify_inner
(
	void *ctx,
	CK_BYTE_PTR pData,
	CK_ULONG ulDataLen,
	CK_BYTE_PTR pSignature,
	CK_ULONG ulSignatureLen
)
{
	char	str[256] = {0};
	printf("%s......\n", __FUNCTION__);
	memcpy(str, pData, ulDataLen);
	printf("input data len: %ld\n", ulDataLen);
	printf("input data : %s\n", pData);
	memset(str, 0, 256);
	memcpy(str, pSignature, ulSignatureLen);
	printf("input sign len: %ld\n", ulSignatureLen);
	printf("input sign : %s\n", str);
	printf("%s ok.\n", __FUNCTION__);
	return CKR_OK;
}

CK_RV	C_GenerateRandom_inner
(
	void *ctx,
	CK_BYTE_PTR pRandomData,
	CK_ULONG ulRandomLen
)
{
	printf("%s......\n", __FUNCTION__);
	printf("ulRandomLen: %ld\n", ulRandomLen);
	memset(pRandomData, 0x33, ulRandomLen);
	printf("%s ok.\n", __FUNCTION__);
	return CKR_OK;
}

CK_RV	C_GenerateKey_inner
(
	void *ctx,
	CK_MECHANISM_PTR pMechanism,
	CK_ATTRIBUTE_PTR pTemplate,
	CK_ULONG ulCount,
	CK_OBJECT_HANDLE_PTR phKey
)
{
	int	i;
	char	str[256] = {0};
	printf("%s......\n", __FUNCTION__);
	printf("pMechanism type: %ld\n", pMechanism->mechanism);
	printf("pMechanism len: %ld\n", pMechanism->ulParameterLen);
	memcpy(str, pMechanism->pParameter, pMechanism->ulParameterLen);
	printf("pMechanism value: %s\n", str);
	printf("ulCount: %ld\n", ulCount);
	for(i = 0; i < ulCount; i++)
	{
		CK_ATTRIBUTE_PTR one = pTemplate + i;
		memset(str, 0, 256);
		printf("CK_ATTRIBUTE type: %ld\n", one->type);
		printf("CK_ATTRIBUTE len: %ld\n", one->ulValueLen);
		memcpy(str, one->pValue, one->ulValueLen);
		printf("CK_ATTRIBUTE value: %s\n", str); 
	}
	*phKey = (CK_OBJECT_HANDLE)3344520;
	printf("%s ok.\n", __FUNCTION__);
	return CKR_OK;
}

CK_RV	C_GenerateKeyPair_inner
(
	void *ctx,
	CK_MECHANISM_PTR pMechanism,
	CK_ATTRIBUTE_PTR pPublicKeyTemplate,
	CK_ULONG ulPublicKeyAttributeCount,
	CK_ATTRIBUTE_PTR pPrivateKeyTemplate,
	CK_ULONG ulPrivateKeyAttributeCount,
	CK_OBJECT_HANDLE_PTR phPublicKey,
	CK_OBJECT_HANDLE_PTR phPrivateKey
)
{
	int	i;
	char	str[256] = {0};
	printf("%s......\n", __FUNCTION__);
	printf("pMechanism type: %ld\n", pMechanism->mechanism);
	printf("pMechanism len: %ld\n", pMechanism->ulParameterLen);
	memcpy(str, pMechanism->pParameter, pMechanism->ulParameterLen);
	printf("pMechanism value: %s\n", str);
	printf("ulPublicKeyAttributeCount: %ld\n", ulPublicKeyAttributeCount);
	for(i = 0; i < ulPublicKeyAttributeCount; i++)
	{
		CK_ATTRIBUTE_PTR one = pPublicKeyTemplate + i;
		memset(str, 0, 256);
		printf("CK_ATTRIBUTE type: %ld\n", one->type);
		printf("CK_ATTRIBUTE len: %ld\n", one->ulValueLen);
		memcpy(str, one->pValue, one->ulValueLen);
		printf("CK_ATTRIBUTE value: %s\n", str); 
	}
	printf("ulPrivateKeyAttributeCount: %ld\n", ulPrivateKeyAttributeCount);
	for(i = 0; i < ulPrivateKeyAttributeCount; i++)
	{
		CK_ATTRIBUTE_PTR one = pPrivateKeyTemplate + i;
		memset(str, 0, 256);
		printf("CK_ATTRIBUTE type: %ld\n", one->type);
		printf("CK_ATTRIBUTE len: %ld\n", one->ulValueLen);
		memcpy(str, one->pValue, one->ulValueLen);
		printf("CK_ATTRIBUTE value: %s\n", str); 
	}
	*phPublicKey = (CK_OBJECT_HANDLE)7788123;
	*phPrivateKey = (CK_OBJECT_HANDLE)7788321;
	printf("%s ok.\n", __FUNCTION__);
	return CKR_OK;
}

CK_RV	C_CreateObject_inner
(
	void *ctx,
	CK_ATTRIBUTE_PTR pTemplate,
	CK_ULONG ulCount,
	CK_OBJECT_HANDLE_PTR phObject
)
{
	CK_ULONG i;
	char	str[256];
	printf("%s......\n", __FUNCTION__);
	for(i = 0; i < ulCount; i++)
	{
		CK_ATTRIBUTE_PTR one = pTemplate + i;
		memset(str, 0, 256);
		printf("CK_ATTRIBUTE type: %ld\n", one->type);
		printf("CK_ATTRIBUTE len: %ld\n", one->ulValueLen);
		memcpy(str, one->pValue, one->ulValueLen);
		printf("CK_ATTRIBUTE value: %s\n", str); 
	}
	*phObject = (CK_OBJECT_HANDLE)888888;
	printf("%s ok.\n", __FUNCTION__);
	return CKR_OK;
}

CK_RV	C_DestroyObject_inner
(
	void *ctx,
	CK_OBJECT_HANDLE hObject
)
{
	printf("%s......\n", __FUNCTION__);
	printf("object: %ld\n", hObject);
	printf("%s ok.\n", __FUNCTION__);
	return CKR_OK;
}

CK_RV	C_FindObjectsInit_inner
(
	void *ctx,
	CK_ATTRIBUTE_PTR pTemplate,
	CK_ULONG ulCount
)
{
	char    str[256] = {0};
	CK_ULONG i;
	printf("%s......\n", __FUNCTION__);
	for(i = 0; i < ulCount; i++)
	{
		CK_ATTRIBUTE_PTR one = pTemplate + i;
		memset(str, 0, 256);
		printf("CK_ATTRIBUTE type: %ld\n", one->type);
		printf("CK_ATTRIBUTE len: %ld\n", one->ulValueLen);
		memcpy(str, one->pValue, one->ulValueLen);
		printf("CK_ATTRIBUTE value: %s\n", str);
	}
	printf("%s ok.\n", __FUNCTION__);
	return CKR_OK;
}

CK_RV	C_FindObjects_inner
(
	void *ctx,
	CK_ULONG ulMaxObjectCount,
	CK_OBJECT_HANDLE_PTR phObject,
	CK_ULONG_PTR pulObjectCount
)
{
	CK_ULONG i;
	printf("%s......\n", __FUNCTION__);
	printf("ulMaxObjectCount: %ld\n", ulMaxObjectCount);
	for(i = 0; i < 8; i++)
	{
		CK_OBJECT_HANDLE_PTR one = phObject + i;
		*one = (CK_OBJECT_HANDLE)(10000 + i);
	}
	*pulObjectCount = 8;
	printf("%s ok.\n", __FUNCTION__);
	return CKR_OK;
}

CK_RV	C_FindObjectsFinal_inner
(
	void *ctx
)
{
	printf("%s ok......\n", __FUNCTION__);
	printf("%s ok.\n", __FUNCTION__);
	return CKR_OK;
}

CK_RV	C_GetAttributeValue_inner
(
	void *ctx,
	CK_OBJECT_HANDLE hObject,
	CK_ATTRIBUTE_PTR pTemplate,
	CK_ULONG ulCount
)
{
	CK_ULONG i;
	printf("%s......\n", __FUNCTION__);
	printf("ulCount: %ld\n", ulCount);
	for(i = 0; i < ulCount; i++)
	{
		CK_ATTRIBUTE_PTR one = pTemplate + i;
		printf("get attr: %ld\n", one->type);
		if(one->ulValueLen > 10)
		{
			sprintf(one->pValue, "getattr%ld", i);
			one->ulValueLen = strlen((char *)one->pValue);
		}
	}
	printf("%s OK.\n", __FUNCTION__);
	return CKR_OK;
}

CK_RV	C_SetAttributeValue_inner
(
	void *ctx,
	CK_OBJECT_HANDLE hObject,
	CK_ATTRIBUTE_PTR pTemplate,
	CK_ULONG ulCount
)
{
	char str[256];
	CK_ULONG i;
	printf("%s......\n", __FUNCTION__);
	printf("object: %ld\n", hObject);
	for(i = 0; i < ulCount; i++)
	{
		CK_ATTRIBUTE_PTR one = pTemplate + i;
		memset(str, 0, 256);
		printf("CK_ATTRIBUTE type: %ld\n", one->type);
		printf("CK_ATTRIBUTE len: %ld\n", one->ulValueLen);
		memcpy(str, one->pValue, one->ulValueLen);
		printf("CK_ATTRIBUTE value: %s\n", str); 
	}
	printf("%s ok.\n", __FUNCTION__);
	return CKR_OK;
}

CK_RV	C_WrapKey_inner
(
	void *ctx,
	CK_MECHANISM_PTR pMechanism,
	CK_OBJECT_HANDLE hWrappingKey,
	CK_OBJECT_HANDLE hKey,
	CK_BYTE_PTR pWrappedKey,
	CK_ULONG_PTR pulWrappedKeyLen
)
{
	char str[256] = {0};
	printf("%s......\n", __FUNCTION__);
	printf("pMechanism type: %ld\n", pMechanism->mechanism);
	printf("pMechanism len: %ld\n", pMechanism->ulParameterLen);
	memcpy(str, pMechanism->pParameter, pMechanism->ulParameterLen);
	printf("pMechanism value: %s\n", str);
	printf("hWrappingKey = %ld\n", hWrappingKey);
	printf("hKey = %ld\n", hKey);
	*pulWrappedKeyLen = 32;
	memset(pWrappedKey, 0x38, 32);
	printf("%s ok.\n", __FUNCTION__);
	return CKR_OK;
}

CK_RV	C_UnwrapKey_inner
(
	void *ctx,
	CK_MECHANISM_PTR pMechanism,
	CK_OBJECT_HANDLE hUnwrappingKey,
	CK_BYTE_PTR pWrappedKey,
	CK_ULONG ulWrappedKeyLen,
	CK_ATTRIBUTE_PTR pTemplate,
	CK_ULONG ulAttributeCount,
	CK_OBJECT_HANDLE_PTR phKey
)
{
	char str[256] = {0};
	CK_ULONG i;
	printf("%s......\n", __FUNCTION__);
	printf("pMechanism type: %ld\n", pMechanism->mechanism);
	printf("pMechanism len: %ld\n", pMechanism->ulParameterLen);
	memcpy(str, pMechanism->pParameter, pMechanism->ulParameterLen);
	printf("pMechanism value: %s\n", str);
	printf("hUnwrappingKey: %ld\n", hUnwrappingKey);
	printf("ulWrappedKeyLen: %ld\n", ulWrappedKeyLen);
	memcpy(str, pWrappedKey, ulWrappedKeyLen);
	printf("pWrappedKey: %s\n", pWrappedKey);
	for(i = 0; i < ulAttributeCount; i++)
	{
		CK_ATTRIBUTE_PTR one = pTemplate + i;
		memset(str, 0, 256);
		printf("CK_ATTRIBUTE type: %ld\n", one->type);
		printf("CK_ATTRIBUTE len: %ld\n", one->ulValueLen);
		memcpy(str, one->pValue, one->ulValueLen);
		printf("CK_ATTRIBUTE value: %s\n", str);
	}
	*phKey = (CK_OBJECT_HANDLE)99999;
	printf("%s ok.\n", __FUNCTION__);
	return CKR_OK;
}

